<template>
    <el-container>
        <el-main class="wrapper">
            <section class="panel cms-content">
                <div class="panel-body">
                    <div class="cms-btn-group">
                        <el-button type="primary" icon="fa fa-plus" @click="create()" size="small"> 新增</el-button>
                        <el-button type="warning" icon="fa fa-refresh" @click="list()" size="small"> 刷新</el-button>
                        <el-button type="success" icon="fa fa-check" @click="multiAgree()" size="small"> 通过</el-button>
                        <el-button type="danger" icon="fa fa-remove" @click="multiRefuse()" size="small"> 拒绝</el-button>
                        <el-button type="info" icon="fa fa-trash" @click="multiDelete()" size="small"> 删除</el-button>
                    </div>
                    <div class="cms-search-group">
                        <div class="cms-search-item">
                            <el-input size="medium" clearable v-model="params.kwd" placeholder="请输入标题"
                                      class="input-with-select">
                                <el-button type="primary" @click="search()" slot="append"
                                           icon="el-icon-search"></el-button>
                            </el-input>
                        </div>
                        <div class="cms-search-item" style="width: 120px">
                            <el-select size="medium" v-model="params.hot">
                                <el-option label="热门(全部)" value=""></el-option>
                                <el-option label="正常" :value="1"></el-option>
                                <el-option label="热门" :value="2"></el-option>
                            </el-select>
                        </div>
                        <div class="cms-search-item" style="width: 120px">
                            <el-select size="medium" v-model="params.position">
                                <el-option label="推荐(全部)" value=""></el-option>
                                <el-option label="正常" :value="1"></el-option>
                                <el-option label="推荐" :value="2"></el-option>
                            </el-select>
                        </div>
                        <div class="cms-search-item" style="width: 120px">
                            <el-select size="medium" v-model="params.status">
                                <el-option label="状态(全部)" value=""></el-option>
                                <el-option label="正常" :value="1"></el-option>
                                <el-option label="待审核" :value="2"></el-option>
                                <el-option label="未通过" :value="3"></el-option>
                            </el-select>
                        </div>
                        <div class="cms-search-item" style="width: 130px">
                            <el-select size="medium"  v-model="params.catepid">
                                <el-option label="类目(全部)" value=""></el-option>
                                <el-option v-for="cate in cates" :key="cate.id" :label="cate.name"
                                           :value="cate.id"></el-option>
                            </el-select>
                        </div>
                        <div class="cms-search-item">
                            <el-date-picker size="medium"
                                            v-model="date"
                                            align="right"
                                            type="daterange"
                                            unlink-panels
                                            range-separator="至"
                                            start-placeholder="开始日期"
                                            end-placeholder="结束日期"
                                            :picker-options="pickerOptions" @change="pickerChange">
                            </el-date-picker>
                        </div>
                    </div>
                    <el-table v-loading="loading"
                              element-loading-text="努力加载中..."
                              element-loading-spinner="el-icon-loading"
                              @selection-change="handleSelectionChange"
                              tooltip-effect="dark" :data="listData.data" border>
                        <el-table-column align="center" type="selection" width="30"></el-table-column>
                        <el-table-column align="center" prop="id" label="编号" width="60"></el-table-column>
                        <el-table-column align="center" prop="thumb" label="封面" width="120">
                            <template slot-scope="scope">
                                <el-popover placement="right" trigger="click">
                                    <img :src="scope.row.thumb"/>
                                    <el-button slot="reference"
                                               style="border: none;height: 50px;padding: 0;overflow: hidden">
                                        <div v-if="scope.row.hot == 2" class="ribbon">
                                            <div class="wrap"><span class="ribbon6">热</span></div>
                                        </div>
                                        <img title="点击查看原图" alt="点击查看原图" :src="scope.row.thumb"
                                             style="height: 50px;width: 100px"/>
                                    </el-button>
                                </el-popover>
                            </template>
                        </el-table-column>
                        <el-table-column prop="title" :show-overflow-tooltip="true" label="标题" width="250"></el-table-column>
                        <el-table-column align="center" prop="user.truename" label="作者" width="120"></el-table-column>
                        <el-table-column align="center" prop="cate.name" label="类目" width="100"></el-table-column>
                        <el-table-column align="center" prop="praise" label="点赞量" width="80"></el-table-column>
                        <el-table-column align="center" prop="view" label="浏览量" width="80"></el-table-column>
                        <el-table-column align="center" prop="status_text" label="状态" width="80">
                            <template slot-scope="scope"><span v-html="scope.row.status_text"></span></template>
                        </el-table-column>
                        <el-table-column align="center" prop="position_text" label="推荐" width="80">
                            <template slot-scope="scope"><span v-html="scope.row.position_text"></span></template>
                        </el-table-column>
                        <el-table-column align="center" prop="created_at" label="创建日期" width="160"></el-table-column>
                        <el-table-column align="center" prop="updated_at" label="最近更新" width="160"></el-table-column>
                        <el-table-column label="操作">
                            <template slot-scope="scope">
                                <el-button @click="edit(scope.row.id)" icon="fa fa-edit" type="primary" size="mini" plain>
                                    编辑
                                </el-button>
<!--                                <el-button type="warning" icon="fa fa-eye" @click="link(scope.row.id)" size="small" plain> 预览</el-button>-->
                                <el-button @click="destroy(scope.row.id)" icon="fa fa-trash"
                                           type="info" size="mini" plain> 删除
                                </el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <pagination :pageData="listData" :params="params" @list="list"/>
                </div>
            </section>
        </el-main>
    </el-container>
</template>

<script>
    import {mapGetters} from 'vuex';
    import {NewList, NewDestroy,CateAll,NewAgree,NewRefuse} from '@/utils/request';
    import Pagination from '@/components/pagination';
    export default {
        components: {Pagination},
        data() {
            return {
                listData: [],//列表数据
                params: {page: 1, kwd: '',catepid:'',status:'',hot:'',position:'',start: '',end: ''},
                chanels:[],
                multipleSelection:[],//多选项
                cates:[],
                date: '',
                //日期筛选
                pickerOptions: {
                    shortcuts: [{
                        text: '今天',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '昨天',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '一周',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '一个月',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                            picker.$emit('pick', [start, end]);
                        }
                    }]
                },
            }
        },
        activated() {
            this.list();
            this.category();
        },
        computed: {
            ...mapGetters(['loading','upload']),
        },
        methods: {
            link(id){
                window.open('http://liange.bh/new/info', '_blank');
            },
            //时间筛选
            pickerChange() {
                if (this.date) {
                    this.params.start = this.moment(this.date[0]).format('YYYY-MM-DD');
                    this.params.end = this.moment(this.date[1]).format('YYYY-MM-DD');
                } else {
                    this.params.start = '';
                    this.params.end = '';
                }
            },
            // 列表
            category(){
                CateAll({params:{pid:1000}}).then((res)=>{
                    if (res.code) {
                        this.$message.error(res.msg);
                    } else {
                        this.cates = res.data;
                    }
                })
            },
            //新增
            create(){
                this.$router.push({name:'NewUpdate'});
            },
            //编辑
            edit(id){
                this.$router.push({name:'NewUpdate',query:{id:id}});
            },
            // 多选删除
            multiDelete(){
                if(this.multipleSelection.length == 0){
                    this.$message.error('请至少选择一项你要删除的项');return false;
                }
                this.destroy(this.multipleSelection);
            },
            // 多选通过
            multiAgree(){
                if(this.multipleSelection.length == 0){
                    this.$message.error('请至少选择一项你要通过的项');return false;
                }
                NewAgree({id: this.multipleSelection}).then((res) => {
                    if (res.code) {
                        this.$message.error(res.msg);
                    } else {
                        this.list();
                    }
                });
            },
            // 多选拒绝
            multiRefuse(){
                if(this.multipleSelection.length == 0){
                    this.$message.error('请至少选择一项你要拒绝的项');return false;
                }
                NewRefuse({id: this.multipleSelection}).then((res) => {
                    if (res.code) {
                        this.$message.error(res.msg);
                    } else {
                        this.list();
                    }
                });
            },
            handleSelectionChange(val) {
                let select = [];
                if(val.length > 0){
                    val.filter((item)=>{
                        if(select.indexOf(item.id) === -1){
                            select.push(item.id)
                        }
                    });
                    this.multipleSelection = select;
                }else{
                    this.multipleSelection = [];
                }
            },
            //删除
            destroy(id) {
                this.$confirm('确定要删除选择的新闻信息吗？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    customClass: 'cms-message-box',
                    type: 'warning'
                }).then(() => {
                    NewDestroy({id: id}).then((res) => {
                        if (res.code) {
                            this.$message.error(res.msg);
                        } else {
                            this.list();
                        }
                    });
                }).catch(() => {});
            },
            //筛选
            search() {
                this.params.page = 1;
                this.list();
            },
            list() {
                this.$store.dispatch('setLoading', true);
                NewList({params: this.params}).then((res) => {
                    if (res.code) {
                        this.$message.error(res.msg);
                    } else {
                        this.listData = res.data;
                    }
                    this.$store.dispatch('setLoading', false);
                })
            },
        }
    }
</script>